﻿@using AVCare.LMS.Util;
@model AVCare.LMS.Web.Models.FileListModel
@{                                                               
    ViewBag.Title = LanguageHelper.GetString("message.softname");
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section head{
    <link href="@Url.Content("~/Content/storage.css")" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.prompt.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/Scripts/selectMuliteRow.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.popMenu.js")"></script>
    <script type="text/javascript">
        var rowTool = null;
        jQuery(function ($) {
            rowTool = new selectMuliteRow();
            rowTool.selectedRowStyle = "selectedRowStyle";
            var isEdit = false;
            $("#fileList ul li:odd").addClass("odd");   
            $("#fileList ul li:even").addClass("even");
            $("#fileList ul li:last").css({"border-bottom":"1px solid #51B1E1"});
            $("#fileList ul .folder").one("mouseover", function () {
                var li = $(this);
                var buttons = $("<div></div>").addClass("buttons").css({"float": "right","margin-right":"10px","height":"100%"}).hide();
                var btnRename = $("<a></a>").text("重命名").attr("href","javascript:void(0);").addClass("floatButton").prepend("<img src='@Url.Content("~/Content/img/rename.png")' />");
                var btnShare = $("<a></a>").text("共享").attr("href","javascript:void(0);").addClass("floatButton").prepend("<img src='@Url.Content("~/Content/img/share.png")' />");
                var btnDel = $("<a></a>").text("删除").attr("href","javascript:void(0);").addClass("floatButton").prepend("<img src='@Url.Content("~/Content/img/delete.png")' />");
                var btnConfirm = $("<a></a>").text("确定").attr("href","javascript:void(0);").addClass("floatButton");
                var btnCancel = $("<a></a>").text("取消").attr("href","javascript:void(0);").addClass("floatButton");
                var txtName = $("<input type='text' />").css({"float":"left","outline":"none","border":"none","height":"20px","line-height":"20px"});
                btnRename.click(function(){
                    txtName.val($.trim(li[0].title));
                    li.find(".buttons").hide();
                    li.find(".RenameEdit").show();
                    li.find("a:first").hide();
                    txtName.focus();
                    isEdit = true;
                });
                btnConfirm.click(function(){
                    $.ajax({
                        type: "post",
                        url:"@Url.Action("RenameFolder", "Disk")", 
                        async: true,
                        cache: false,
                        data: {newName:txtName.val(),id:li.attr("id")}
                    });
                    li.find("a")[0].lastChild.nodeValue = txtName.val();
                    li.attr("title",txtName.val());

                    li.find(".buttons").show();
                    li.find(".RenameEdit").hide(); 
                    li.find("a").show();
                    isEdit = false;                 
                });
                btnCancel.click(function(){
                    li.find(".buttons").show();
                    li.find(".RenameEdit").hide(); 
                    li.find("a").show();
                    isEdit = false;     
                });
                btnDel.click(function(){
                    var txt = "确定删除文件夹 "+li.find("a:first").text();
                    $.prompt(txt, {
                        buttons: [
                            { 
                                name: "确定", 
                                closeWindow: false, 
                                click: function () {
                                    $.ajax({
                                        type: "post",
                                        url:"@Url.Action("DeleteFolder", "Disk")", 
                                        async: true,
                                        cache: false,
                                        data: {id:li.attr("id")},
                                        success:function(){
                                            location.reload();    
                                        }
                                    });    
                                }
                            },
                            { 
                                name: "关闭", 
                                closeWindow: true
                            }
                        ]
                    });    
                });
                
                btnShare.click(function(){  
                    var container = $("<div></div>");
                    container.css({"width":"250px"});
                    var p1 = $("<p>不共享</p>");
                    var noshare = $('<input type="radio" class="styled" name="shareType" id="@ShareRole.NoShare" value="@Convert.ToInt32(ShareRole.NoShare)" />');
                    var p2 = $("<p>完全共享</p>");
                    var share = $('<input type="radio" class="styled" name="shareType" id="@ShareRole.Full" value="@Convert.ToInt32(ShareRole.Full)" />');
                    var p3 = $("<p>密码共享</p>");
                    var sharebyPass = $('<input type="radio" class="styled" name="shareType" id="@ShareRole.Password" value="@Convert.ToInt32(ShareRole.Password)" />');
                    var inputarea = $("<div id='inputarea'></div>").css({"width":"100%","height":"80px","border":"1px solid #9EBFE1","padding":"5px","background":"#C8E4FA","position":"relative"});
                    inputarea.attr("disabled",true);                                                 
                    var mask = $("<div id='mask'></div>").css({"width":"100%","height":"100%","z-index":"999","background":"#fff","position":"absolute","margin":"-5px","opacity":"0.6","filter":"alpha(opacity=60)" });
                    var txtpassword = $("<p><span style='float:left; width:80px;'>密码：</span><input id='txtpassword' type='password' class='styled' /></p>");
                    var txtconfirm = $("<br /><p><span style='float:left;width:80px;'>确认密码</span><input id='txtconfirm' type='password' class='styled' /></p>");
                    inputarea.append(mask);
                    inputarea.append(txtpassword);
                    inputarea.append(txtconfirm);
                    p1.prepend(noshare);
                    p2.prepend(share);
                    p3.prepend(sharebyPass);
                    container.append(p1);                                                                                                    
                    container.append(p2);                                                           
                    container.append(p3);
                    container.append(inputarea);
                    
                    $.prompt(container[0].outerHTML, {
                        buttons: [
                            { 
                                name: "确定", 
                                closeWindow: true, 
                                click: function () {
                                    $(":radio[checked=true]").attr("value");                                    
                                    $.ajax({
                                        type: "post",
                                        url:"@Url.Action("shareFolder", "Disk")", 
                                        async: true,
                                        cache: false,
                                        data: {id:li.attr("id"),role:$(":radio[checked=true]").attr("value"),password:txtpassword.val()},
                                        success:function(){
                                                
                                        }
                                    });    
                                }
                            },
                            { 
                                name: "关闭", 
                                closeWindow: true
                            }
                        ],
                        loaded:function(){
                             $.ajax({
                                type: "post",
                                url:"@Url.Action("ShareType", "Disk")", 
                                async: true,
                                cache: false,
                                data: {id:li.attr("id")},
                                success:function(msg){
                                    $("input[name=shareType]").each(function(){
                                        if(this.value == msg){
                                            this.checked = true;
                                            this.select();
                                        }
                                    });
                                    
                                    Custom.init(); 

                                    $("input[name=shareType]").prev("span").click(function(){
                                        if ($("#Password").attr("checked")) {
                                            $("#mask").hide();
                                            $("#inputarea").attr("disabled",false);
                                        }else{
                                            $("#mask").show();
                                            $("#inputarea").attr("disabled",true);
                                            $("#txtpassword").val("");
                                            $("#txtconfirm").val("");    
                                        }
                                    });
                                }
                            });
                        }
                    });       
                    
                    
                });
                
                buttons.append(btnRename);
                buttons.append(btnShare);
                buttons.append(btnDel);
                var div = $("<div></div>").addClass("RenameEdit").css({"float":"left","margin":"10px","height":"20px","line-height":"20px"});
                div.append(txtName);
                div.append(btnConfirm); 
                div.append(btnCancel);
                div.hide();
                li.append(div);

                li.append(buttons); 
            });
            
            $("#fileList ul .file").one("mouseover", function () {
                var li = $(this);
                var buttons = $("<div></div>").addClass("buttons").css({"float": "right","margin-right":"10px","height":"100%"}).hide();
                var btnRename = $("<a></a>").text("重命名").attr("href","javascript:void(0);").addClass("floatButton").prepend("<img src='@Url.Content("~/Content/img/rename.png")' />");
                var btnDel = $("<a></a>").text("删除").attr("href","javascript:void(0);").addClass("floatButton").prepend("<img src='@Url.Content("~/Content/img/delete.png")' />");
                var btnConfirm = $("<a></a>").text("确定").attr("href","javascript:void(0);").addClass("floatButton");
                var btnCancel = $("<a></a>").text("取消").attr("href","javascript:void(0);").addClass("floatButton");
                var txtName = $("<input type='text' />").css({"float":"left","outline":"none","border":"none","height":"20px","line-height":"20px"});
                var extName;
                btnRename.click(function(){
                    var name = $.trim(li.find("a:first").text());
                    var disName = name.substring(0,name.lastIndexOf("."))
                    txtName.val(disName);
                    li.find(".buttons").hide();
                    li.find(".RenameEdit").show();
                    li.find("a:first").hide();
                    txtName.focus();
                    extName = name.substring(name.lastIndexOf("."));
                    isEdit = true;
                });
                btnConfirm.click(function(){
                    var fileName = txtName.val();
                    if(fileName.substring(fileName.lastIndexOf(".")) != extName){
                       fileName = fileName + extName;             
                    }
                    $.ajax({
                        type: "post",
                        url:"@Url.Action("RenameFile", "Disk")", 
                        async: true,
                        cache: false,
                        data: {newName:fileName,id:li.attr("id")}
                    });
                    li.find("a")[0].lastChild.nodeValue = fileName;
                    li.attr("title",fileName);

                    li.find(".buttons").show();
                    li.find(".RenameEdit").hide(); 
                    li.find("a").show();
                    isEdit = false;                 
                });
                
                btnCancel.click(function(){
                    li.find(".buttons").show();
                    li.find(".RenameEdit").hide(); 
                    li.find("a").show();
                    isEdit = false;     
                });
                btnDel.click(function(){
                    var txt = "确定删除文件 "+li.find("a:first").text();
                    $.prompt(txt, {
                        buttons: [
                            { 
                                name: "确定", 
                                closeWindow: false, 
                                click: function () {
                                    $.ajax({
                                        type: "post",
                                        url:"@Url.Action("DeleteFile", "Disk")", 
                                        async: true,
                                        cache: false,
                                        data: {id:li.attr("id")},
                                        success:function(){
                                            location.reload();    
                                        }
                                    });    
                                }
                            },
                            { 
                                name: "关闭", 
                                closeWindow: true
                            }
                        ]
                    });
                });

                buttons.append(btnRename);
                buttons.append(btnDel);
                var div = $("<div></div>").addClass("RenameEdit").css({"float":"left","margin":"10px","height":"20px","line-height":"20px"});
                div.append(txtName);
                div.append(btnConfirm); 
                div.append(btnCancel);
                div.hide();
                li.append(div);

                li.append(buttons); 
            });
            $("#fileList ul li").hover(
                function () {
                    if (isEdit) {
                        return;
                    }
                    $("#fileList ul li div.buttons").hide();
                    $(this).find("div.buttons").fadeIn(500);
                },
                function () {
                     $("#fileList ul li div.buttons").hide();
                }
            );

            $("#fileList ul li[value]").click(function(){
                if(rowTool.selectRow(this)){
                    $.ajax({
                        type: "post",
                        url:"@Url.Action("RenameFile", "Disk")".replace("RenameFile",this.option), 
                        async: true,
                        cache: false,
                        data: {id:this.id},
                        success:function(msg){
                            if(msg.type == "folder"){                                                                              
                                $("#folderInfo").find("#foldername").text("名称：" + msg.name); 
                                $("#folderInfo").find("#username").text("用户：" + msg.user);
                                $("#folderInfo").show();
                                $("#fileInfo").hide();
                            }
                            if(msg.type == "file"){ 
                                $("#fileInfo").find("#filename").text("文件名：" + msg.name);          
                                $("#fileInfo").find("#modifydate").text("修改时间：" + msg.lastEditTime);
                                $("#fileInfo").find("#size").text("大小：" + msg.size);     
                                $("#fileInfo").find("#md5").text("MD5：" + msg.md5);
                                $("#fileInfo").find("#username").text("用户：" + msg.user);
                                $("#folderInfo").hide();
                                $("#fileInfo").show();
                            }
                        }
                    });
                }
            });
            $("#btnUpload").click(function () {
                var isUpload = false;
                var txt = '<object id="uploader" classid="CLSID:0B400360-C488-4CA4-AC58-BA46BE5853D1" codebase="@Url.Content("~/Content/FileUpload.cab")#version=1,0,0,1"  style="width:500px; height:300px;" >';
                $.prompt(txt, {
                    buttons: [
                        { 
                            name: "上传", 
                            closeWindow: false, 
                            click: function () {
                                var object = $("#uploader")[0];
                                object.StartUpload();
                                isUpload = true;
                            }
                        },
                        { 
                            name: "关闭", 
                            closeWindow: true,
                            click: function () {if (isUpload) {location.reload();}}
                        }
                    ],
                    loaded: function () {     
                        var object = $("#uploader")[0];
                        object.SetDNSPort(location.hostname, 10001);
                        bool = object.SetUploadTypeName("文件", "文件夹");
                        bool = object.SetUploadPath("@Path.Combine(SystemConfigurationManager.FileRoot, User.Identity.Name, Model.CurrentFolder.Path).Replace(@"\", @"\\")");
                        bool = object.SetMultiDirSeries(-1);
                        bool = object.SetFilterFileName(".*");
                        bool = object.ShowUploadType(true, 3);

                        object.attachEvent("UploadOneByOne", function (pFileName, lStatus) {
                            $.ajax({
                                type: "post",
                                url:"@Url.Action("Upload", "Disk")", 
                                async: false,
                                cache: false,
                                data: {filename:pFileName,currentid:"@Model.CurrentFolder.ID",currentPath:"@HttpUtility.UrlEncode(Path.Combine(SystemConfigurationManager.FileRoot, User.Identity.Name, Model.CurrentFolder.Path))"}
                            });
                        });
                        object.attachEvent("UploadFinished", function (bSucc) {
                            
                        });
                        object.attachEvent("ServerStatus", function (lServerStatus) {
                            
                        });
                        object.attachEvent("UploadTypeChanged", function (lUploadType) {

                        });
                    }
                });
            });

            $("#btnNewFolder").click(function(){
                var txt = '<div>新建文件夹</div><input type="text" id="txtNewFolder" maxlength="20" />';
                $.prompt(txt, {
                    buttons: [
                        { 
                            name: "确定", 
                            closeWindow: true, 
                            click: function () {
                                $.ajax({
                                    type: "post",
                                    url:"@Url.Action("NewFolder", "Disk")", 
                                    async: true,
                                    cache: false,
                                    data: {foldername:$("#txtNewFolder").val(),parentid:"@Model.CurrentFolder.ID"},
                                    success:function(msg){
                                        location.reload();
                                    }
                                });               
                            }
                        },
                        { 
                            name: "关闭", 
                            closeWindow: true
                        }
                    ]
                });
            });
            

        });
    </script>
}
<div id="main">
    <div id="right">
        @{Html.RenderAction("LoginPartial", "User");}
        <div class="fileInfo" id="diskInfo">
            <span class="infoTitle">空间信息</span>
            <ul class="detail">
                <li>用户名：</li>
                <li>总大小：</li>
                <li>可用空间：</li>
            </ul>
        </div>
        <div class="fileInfo" id="fileInfo" style="display: none;">
            <span class="infoTitle">文件信息</span>
            <ul class="detail">
                <li id="filename"></li>
                <li id="modifydate"></li>
                <li id="size"></li>
                <li id="md5"></li>
                <li id="username"></li>
            </ul>
        </div>
        <div class="fileInfo" id="folderInfo" style="display: none;">
            <span class="infoTitle">文件夹信息</span>
            <ul class="detail">
                <li>文件夹</li>
                <li id="foldername"></li>
                <li id="username"></li>
            </ul>
        </div>
        <div class="fileInfo" style="display: none;">
            <span class="infoTitle">共享信息</span>
            <ul class="detail">
                <li>共享方式：</li>
            </ul>
        </div>
    </div>
    <div id="container">
        <div id="tools">
            <div class="btnClass">
                <input type="button" class="styled" id="btnUpload" value="上传" />
            </div>
            <div class="btnClass">
                <!--input type="button" class="styled" id="btnNewFolder" style="width: 80px;" value="新建文件夹" /-->
                <a href="javascript:void(0);" id="btnNewFolder" title="新建文件夹" class="whiteColor">
                    <img style="width:16px; height:16px;" src="@Url.Content("~/content/img/newfolder.png")" class="fileIcon" />新建文件夹</a>
            </div>
        </div>
        <div id="fileList">
            <div id="header">
            </div>
            <ul>
                <li><a href="@Url.Action("Index", "Disk", new { id = Model.RootID })" title="返回根目录" class="whiteColor">
                    <img src="@Url.Content("~/content/img/fti/folder.png")" class="fileIcon" />.</a></li>
                <li><a href="@Url.Action("Index", "Disk", new { id = Model.ParentID })" title="返回上级目录" class="whiteColor">
                    <img src="@Url.Content("~/content/img/fti/folder.png")" class="fileIcon" />..</a></li>
                @{
                    var i = 0;
                    if (Model.ShowShare)
                    {
                    <li><a href="@Url.Action("Sharedfolder", "Disk")" title="共享文件夹" class="whiteColor">
                        <img src="@Url.Content("~/content/img/fti/folder.png")" class="fileIcon" />共享文件夹</a></li>
                    }        
                
                }
                @foreach (var item in Model.Folders)
                {
                
                    <li class="folder" title="@item.Name" option="FolderInfo" id="@item.ID" value="@i" >
                        <a href="@Url.Action("Index", "Disk", new { id = item.ID })" class="whiteColor">
                            <img src="@Url.Content("~/content/img/fti/folder.png")" class="fileIcon" />@item.Name</a>
                    </li>
                    i++;
                }
                @foreach (var item in Model.Files)
                {
                    <li class="file" title="@item.FileName" option="FileInfo" id="@item.ID" value="@i"  >
                        <a href="@Url.Action("Downdoad", "Disk", new { id = item.ID })" class="whiteColor">
                            <img src="@{
                    string path = Url.Content("~/content/img/fti/" + item.FileName.Substring(item.FileName.LastIndexOf(".") + 1) + ".png");
                    if (File.Exists(Server.MapPath(path)))
                    {
                        @path;
                    }
                    else
                    {
                        @Url.Content("~/content/img/fti/folder.png");
                    }}" class="fileIcon" />@item.FileName</a> </li>
                    i++;
                }
            </ul>
        </div>
        <div id="bottom">
        </div>
    </div>
</div>
